package com.wing.order.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.common.utils.Result;
import com.wing.enterprise.feign.EnterpriseClient;
import com.wing.enterprise.invoicing.model.entity.TransportCompany;
import com.wing.order.model.response.*;
import com.wing.order.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/**
 *
 * @author
 */
@Validated
@Api(value = "Admin-云仓入库作业记录", tags = "#/order/cloudInWorkRecord")
@RestController("cloudInWorkRecordController")
@RequestMapping("order/admin/cloudInWorkRecord")
@RequiredArgsConstructor
public class CloudInWorkRecordController {

    private final InventoryOutPlanService inventoryOutPlanService;
    private final InventoryOutPlanDetailService inventoryOutPlanDetailService;
    private final EnterpriseClient enterpriseClient;
    private final InventoryInoutJobService inventoryInoutJobService;
    private final InventoryJobLabelService inventoryJobLabelService;
    private final InventoryJobProductService inventoryJobProductService;
    private final InventoryJobService inventoryJobService;
    @ApiOperation(value = "查询所有云仓入库作业记录", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<InventoryInoutJobVO> list(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "asc") String order
            ,InventoryInoutJobVO inventoryInoutJobVO
    ){
        //出库类型是调拨到云仓
        inventoryInoutJobVO.setInoutTypeCode("allotCloudWarehouse");
        //搜索
        IPage<InventoryInoutJobVO> list = inventoryOutPlanService.getInventoryOutWorkRecordPageList(inventoryInoutJobVO,page,limit);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @GetMapping("/getInventoryOutPlanById")
    public JsonResult getInventoryOutPlanById(@RequestParam(value = "id") Long id){
        InventoryOutPlanVO info = inventoryOutPlanService.getInventoryOutPlanById(id);
        return JsonResult.success(info);
    }

    @ApiOperation(value = "查询出库产品信息详情", notes = "添加")
    @GetMapping("/getOutWorkProductDetail")
    public JsonResult getOutWorkProductDetail(@RequestParam(value = "jobId") Long jobId){
        List<InventoryJobProductVO> productVOList = inventoryJobProductService.getOutWorkProductDetail(jobId);
        for (InventoryJobProductVO item : productVOList){
            item.setTotalMoney(new BigDecimal(item.getQuantity()).multiply(item.getTotal()).setScale(2,BigDecimal.ROUND_HALF_UP));
        }
        return JsonResult.success(productVOList);
    }

    @ApiOperation(value = "查询出库作业记录产品明细的包装码数据", notes = "查询")
    @GetMapping("/getOutWorkPackList")
    public JsonResult getOutWorkPackList(@RequestParam(value = "jobId") Long jobId,@RequestParam(value = "jobProductId") Long jobProductId){
        List<InventoryJobLabelVO> outWorkPackList = inventoryJobLabelService.getOutWorkPackList(jobId,jobProductId);
        return JsonResult.success(outWorkPackList);
    }
    @ApiOperation(value = "查询云仓入库作业记录详情", notes = "查询")
    @GetMapping("/getInventoryJobVOByJobId")
    public JsonResult getInventoryJobVOByJobId(@RequestParam(value = "jobId") Long jobId){
        InventoryJobVO info = inventoryJobService.getInventoryJobVOByJobId(jobId);
        JsonResult<TransportCompany> companyJsonResult = enterpriseClient.getTransportCompanyById(info.getCompanyId());
        if (companyJsonResult.getCode() == Result.Code.SUCCESS.getCode()) {
            info.setLogistics(companyJsonResult.getData().getName());
        }
        return JsonResult.success(info);
    }
}
